package com.example.musicwebcon.service;

import com.example.musicwebcon.model.dto.songDTO;
import com.example.musicwebcon.model.song;
import com.example.musicwebcon.repository.songRepository;
import com.example.musicwebcon.service.serviceInterface.IsongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class songService implements IsongService {
    @Autowired
    songRepository songRepository;
    @Override
    public song add(songDTO song) {
        song Song = new song();
        BeanUtils.copyProperties(song,Song);
        return songRepository.save(Song);
    }

    @Override
    public Iterable<song> addBatch(Iterable<songDTO> songs) {
        Iterable<song> Songs = new ArrayList<>();
        for(songDTO dto:songs){
            song Song = new song();
            BeanUtils.copyProperties(dto,Song);
            ((List<song>)Songs).add(Song);
        }
        return songRepository.saveAll(Songs);
    }

    @Override
    public song getMusicId(String musicId) {
        return songRepository.findById(musicId).orElseThrow(() -> {
            throw new IllegalArgumentException("歌曲不存在，参数异常！");
        });
    }

    @Override
    public Iterable<song> getAllBysongListId(String songListId) {
        return songRepository.findBySongListId(songListId);
    }

    @Override
    public Iterable<song> getAllByuserId(String userId) {
        return songRepository.findByUserId(userId);
    }

    @Override
    public song edit(songDTO song) {
        song Song = new song();
        BeanUtils.copyProperties(song,Song);
        return songRepository.save(Song);
    }

    @Override
    public void deleteMusicId(String musicId) {
        songRepository.deleteById(musicId);
    }

    @Override
    public void deleteSongListId(String songListId) {
        songRepository.deleteBySongListId(songListId);
    }
}
